<?php

/**
 * This is the model class for table "employee".
 *
 * The followings are the available columns in table 'employee':
 * @property string $EMPLOYEE_ID
 * @property integer $ORG_ID
 * @property integer $JOB_TITLE_ID
 * @property string $FIRST_NAME
 * @property string $LAST_NAME
 * @property string $PREFERRED_NAME
 * @property string $EMAIL
 * @property string $MOBILE_NUMBER
 * @property string $WORK_PHONE_NUMBER
 * @property string $NATIONALLITY
 * @property string $CURRENT_WORK_LOCATION
 * @property string $DEVELOPMENT_TITLE
 * @property string $TIME_CREATED
 * @property integer $USER_CREATED
 * @property string $TIME_UPDATED
 * @property integer $USER_UPDATED
 * @property integer $USE_IMAGE_PROFILE
 * @property string $IMAGE_PROFILE
 *
 * The followings are the available model relations:
 * @property JobTitle $jOBTITLE
 * @property Organization $oRG
 */
class Employee extends CMasterModel {

    public $IMAGE_DATA = null;
    public $DELETE_IMAGE = 0;
    public $ACTIVATE_AS_USER = true;

    /**
     * Returns the static model of the specified AR class.
     * @return Employee the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'employee';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('ORG_ID, JOB_TITLE_ID, USER_CREATED, USER_UPDATED', 'required'),
            array('ORG_ID, JOB_TITLE_ID, USER_CREATED, USER_UPDATED, USE_IMAGE_PROFILE', 'numerical', 'integerOnly' => true),
            array('EMPLOYEE_ID', 'length', 'max' => 15),
            array('EMPLOYEE_ID,FIRST_NAME, LAST_NAME', 'required'),
            array('FIRST_NAME, LAST_NAME, NATIONALLITY', 'length', 'max' => 100),
            array('PREFERRED_NAME, CURRENT_WORK_LOCATION, DEVELOPMENT_TITLE', 'length', 'max' => 200),
            array('EMAIL', 'length', 'max' => 128),
            array('MOBILE_NUMBER, WORK_PHONE_NUMBER', 'length', 'max' => 50),
            array('IMAGE_PROFILE', 'length', 'max' => 40),
            array('TIME_CREATED, TIME_UPDATED', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('EMPLOYEE_ID, ORG_ID, JOB_TITLE_ID, FIRST_NAME, LAST_NAME, PREFERRED_NAME, EMAIL, MOBILE_NUMBER, WORK_PHONE_NUMBER, NATIONALLITY, CURRENT_WORK_LOCATION, DEVELOPMENT_TITLE, TIME_CREATED, USER_CREATED, TIME_UPDATED, USER_UPDATED, USE_IMAGE_PROFILE, IMAGE_PROFILE', 'safe', 'on' => 'search'),
            array('IMAGE_DATA', 'file', 'types' => 'jpg', 'allowEmpty' => true),
            array('DELETE_IMAGE',
                'boolean',
                'falseValue' => 0,
                'trueValue' => 1,
            ),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'jOBTITLE' => array(self::BELONGS_TO, 'JobTitle', 'JOB_TITLE_ID'),
            'oRG' => array(self::BELONGS_TO, 'Organization', 'ORG_ID'),
            'profile' => array(self::HAS_ONE, 'UserProfiles', 'EMPLOYEE_ID'),
            'user' => array(CActiveRecord::HAS_ONE, 'User', 'USER_ID', 'through' => 'profile'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'EMPLOYEE_ID' => 'Employee',
            'ORG_ID' => 'Org',
            'JOB_TITLE_ID' => 'Job Title',
            'FIRST_NAME' => 'First Name',
            'LAST_NAME' => 'Last Name',
            'PREFERRED_NAME' => 'Preferred Name',
            'EMAIL' => 'Email',
            'MOBILE_NUMBER' => 'Mobile Number',
            'WORK_PHONE_NUMBER' => 'Work Phone Number',
            'NATIONALLITY' => 'Nationallity',
            'CURRENT_WORK_LOCATION' => 'Current Work Location',
            'DEVELOPMENT_TITLE' => 'Development Title',
            'TIME_CREATED' => 'Time Created',
            'USER_CREATED' => 'User Created',
            'TIME_UPDATED' => 'Time Updated',
            'USER_UPDATED' => 'User Updated',
            'USE_IMAGE_PROFILE' => 'Use Image Profile',
            'IMAGE_PROFILE' => 'Image Profile',
            'DELETE_IMAGE' => 'Delete Photo',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('EMPLOYEE_ID', $this->EMPLOYEE_ID, true);
        $criteria->compare('ORG_ID', $this->ORG_ID);
        $criteria->compare('JOB_TITLE_ID', $this->JOB_TITLE_ID);
        $criteria->compare('FIRST_NAME', $this->FIRST_NAME, true);
        $criteria->compare('LAST_NAME', $this->LAST_NAME, true);
        $criteria->compare('PREFERRED_NAME', $this->PREFERRED_NAME, true);
        $criteria->compare('EMAIL', $this->EMAIL, true);
        $criteria->compare('MOBILE_NUMBER', $this->MOBILE_NUMBER, true);
        $criteria->compare('WORK_PHONE_NUMBER', $this->WORK_PHONE_NUMBER, true);
        $criteria->compare('NATIONALLITY', $this->NATIONALLITY, true);
        $criteria->compare('CURRENT_WORK_LOCATION', $this->CURRENT_WORK_LOCATION, true);
        $criteria->compare('DEVELOPMENT_TITLE', $this->DEVELOPMENT_TITLE, true);
        $criteria->compare('TIME_CREATED', $this->TIME_CREATED, true);
        $criteria->compare('USER_CREATED', $this->USER_CREATED);
        $criteria->compare('TIME_UPDATED', $this->TIME_UPDATED, true);
        $criteria->compare('USER_UPDATED', $this->USER_UPDATED);
        $criteria->compare('USE_IMAGE_PROFILE', $this->USE_IMAGE_PROFILE);
        $criteria->compare('IMAGE_PROFILE', $this->IMAGE_PROFILE, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    protected function afterSave() {
        parent::afterSave();

        if ($this->ACTIVATE_AS_USER == true) {

            //get user by employee id
        }
    }

}